package com.lrkj.property.service.impl;

import java.util.List;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.property.mapper.CommunityParamMapper;
import com.lrkj.property.domain.CommunityParam;
import com.lrkj.property.service.ICommunityParamService;

/**
 * 小区参数Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-28
 */
@Service
public class CommunityParamServiceImpl implements ICommunityParamService
{
    @Autowired
    private CommunityParamMapper communityParamMapper;

    /**
     * 查询小区参数
     *
     * @param id 小区参数ID
     * @return 小区参数
     */
    @Override
    public CommunityParam selectCommunityParamById(String id)
    {
        return communityParamMapper.selectCommunityParamById(id);
    }

    /**
     * 查询小区参数列表
     *
     * @param communityParam 小区参数
     * @return 小区参数
     */
    @Override
    public List<CommunityParam> selectCommunityParamList(CommunityParam communityParam)
    {
        return communityParamMapper.selectCommunityParamList(communityParam);
    }

    /**
     * 新增小区参数
     *
     * @param communityParam 小区参数
     * @return 结果
     */
    @Override
    public int insertCommunityParam(CommunityParam communityParam)
    {
        communityParam.setId(IdUtils.simpleUUID());
        communityParam.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        communityParam.setCreateTime(DateUtils.getNowDate());
        return communityParamMapper.insertCommunityParam(communityParam);
    }

    /**
     * 修改小区参数
     *
     * @param communityParam 小区参数
     * @return 结果
     */
    @Override
    public int updateCommunityParam(CommunityParam communityParam)
    {
        communityParam.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        communityParam.setUpdateTime(DateUtils.getNowDate());
        return communityParamMapper.updateCommunityParam(communityParam);
    }

    /**
     * 批量删除小区参数
     *
     * @param ids 需要删除的小区参数ID
     * @return 结果
     */
    @Override
    public int deleteCommunityParamByIds(String[] ids)
    {
        return communityParamMapper.deleteCommunityParamByIds(ids);
    }

    /**
     * 删除小区参数信息
     *
     * @param id 小区参数ID
     * @return 结果
     */
    @Override
    public int deleteCommunityParamById(String id)
    {
        return communityParamMapper.deleteCommunityParamById(id);
    }

    @Override
    public int saveOrUpdate(CommunityParam communityParam) {
        CommunityParam oldCommunityParam = communityParamMapper.selectCommunityParamByKey(communityParam.getCommunityId(), communityParam.getParamKey());
        if(null != oldCommunityParam){
            oldCommunityParam.setParamValue(communityParam.getParamValue());
            this.updateCommunityParam(oldCommunityParam);
        }else{
            this.insertCommunityParam(communityParam);
        }
        return 1;
    }

    /**
     * 批量新增小区参数信息
     *
     * @param communityParams 小区参数对象
     */
    public int batchAddData(List<CommunityParam> communityParams)
    {
           return communityParamMapper.batchAddData(communityParams);
    }
}
